package com.hongxun.modules.bss.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hongxun.modules.bss.entity.RequstAPointEntity;
import com.hongxun.modules.bss.service.DDeviceBuyService;
import com.hongxun.modules.bss.service.DDeviceOldService;
import com.hongxun.modules.eys.entity.EPointEntity;
import com.hongxun.modules.eys.entity.RequstEPointEntity;
import org.apache.shiro.authz.annotation.RequiresPermissions;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hongxun.modules.bss.entity.DPointEntity;
import com.hongxun.modules.bss.service.DPointService;
import com.hongxun.common.utils.PageUtils;
import com.hongxun.common.utils.Query;
import com.hongxun.common.utils.R;




/**
 * 项目点位采集基础表
 * 
 * @author zkcn
 * @email youeremail@163.com
 * @date 2018-08-30 14:46:00
 */
@RestController
@RequestMapping("/bss/dpoint")
public class DPointController {
	@Autowired
	private DPointService dPointService;
	@Autowired
	private DDeviceBuyService dDeviceBuyService;
	@Autowired
	private DDeviceOldService dDeviceOldService;

	/**
	 * 列表
	 */
	@RequestMapping("/list")
	public R list(@RequestParam Map<String, Object> params){
		System.out.print(params);
		//查询列表数据
        Query query = new Query(params);
        String wheres =" 1=1 ";

		if (query.get("pGuid") != null && !query.get("pGuid").toString().equals("")) {
			wheres += " and  p_guid = '" + query.get("pGuid").toString() + "' ";
		}else{
			wheres += " and  1=2 ";
		}
		if (query.get("pPCode") != null && !query.get("pPCode").toString().equals("")) {
			wheres += " and  p_p_code like '%" + query.get("pPCode").toString() + "%' ";
		}
		if (query.get("pType") != null && !query.get("pType").toString().equals("")) {
			wheres += " and  p_p_name = '" + query.get("pType").toString() + "' ";
		}
		if (query.get("voltage") != null && !query.get("voltage").toString().equals("")) {
			wheres += " and  p_p_voltage = '" + query.get("voltage").toString() + "' ";
		}
		if (query.get("range") != null && !query.get("range").toString().equals("")) {
			wheres += " and  p_r_guid = '" + query.get("range").toString() + "' ";
		}

		wheres+=" and  p_p_datastate!= '删除' ";
        query.put("filterSql",wheres+" ");
		List<DPointEntity> dPointList = dPointService.queryList(query);
		List<RequstAPointEntity> aPointEntities = new ArrayList<>();

		Map<String, Object> map = new HashMap<>();
		for (DPointEntity e : dPointList) {
			RequstAPointEntity r = new RequstAPointEntity();
			map.put("filterSql", "p_p_guid ='" + e.getGuid() + "'");
			int buyCount = dDeviceBuyService.queryTotal(map);
			int oldCount = dDeviceOldService.queryTotal(map);
			BeanUtils.copyProperties(e, r);
			r.setDeviceCount(buyCount + oldCount);
			aPointEntities.add(r);
		}





		int total = dPointService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(aPointEntities, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}
	@RequestMapping("/select")
	public R select(@RequestParam Map<String, Object> params){
		//查询列表数据
		Query query = new Query(params);
		String wheres =" 1=1 ";
		if(query.get("pGuid")!=null&&!query.get("pGuid").toString().equals(""))
		{
			wheres+=" and  p_guid = '" + query.get("pGuid").toString()+"' ";
		}
		if(query.get("dpGuid")!=null&&!query.get("dpGuid").toString().equals(""))
		{
			wheres+=" and guid = '" + query.get("dpGuid").toString()+"' ";
		}

		wheres+=" and  p_p_datastate != '删除' ";
		query.put("filterSql",wheres+" ");
		List<DPointEntity> dPointList = dPointService.queryList(query);
		return R.ok().put("returnlist", dPointList);
	}

	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{guid}")
	public R info(@PathVariable("guid") String guid){
		DPointEntity dPoint = dPointService.queryObject(guid);
		
		return R.ok().put("dPoint", dPoint);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	public R save(@RequestBody DPointEntity dPoint){
		dPointService.save(dPoint);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	public R update(@RequestBody DPointEntity dPoint){
		dPointService.update(dPoint);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	public R delete(@RequestBody String[] guids){
		dPointService.deleteBatch(guids);
		
		return R.ok();
	}
	
}
